package com.youi.service.impl;

import com.google.protobuf.TextFormat.ParseException;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.dao.UserorderDao;
import com.youi.entity.Expressnumber;
import com.youi.entity.Merchant;
import com.youi.entity.Orderaftersale;
import com.youi.entity.Ordergoods;
import com.youi.entity.Orderprofitdetail;
import com.youi.entity.Sysuser;
import com.youi.entity.Userorder;
import com.youi.entity.Userorderexcel;
import com.youi.entity.Yibaopayment;
import com.youi.entity.vo.ExportMerchantOrderVO;
import com.youi.entity.vo.ExportOrderVO;
import com.youi.entity.vo.ExportPlatformOrderVO;
import com.youi.entity.vo.UserorderexcelVo;
import com.youi.manage.HttpSessionManage;
import com.youi.model.ExceloutResult;
import com.youi.service.ExpressnumberService;
import com.youi.service.GoodsService;
import com.youi.service.GoodtypeService;
import com.youi.service.MerchantService;
import com.youi.service.OrderaftersaleService;
import com.youi.service.OrdergoodsService;
import com.youi.service.OrderprofitdetailService;
import com.youi.service.SetleaderlevelService;
import com.youi.service.UserorderService;
import com.youi.service.UserorderexcelService;
import com.youi.service.YibaopaymentService;
import com.youi.third.ali.oss.OSSUtil;
import com.youi.util.ArithUtils;
import com.youi.util.DateUtil;
import com.youi.util.FileUploadUtil;
import com.youi.util.LogUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.SmsUtil;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class UserorderServiceImpl extends BaseServiceImpl<Userorder> implements UserorderService {

    private UserorderDao userorderDao;
    @Autowired
    private OrdergoodsService ordergoodsService;
    @Autowired
    private OrderaftersaleService orderaftersaleService;
    @Autowired
    private ExpressnumberService expressnumberService;
    @Autowired
    private GoodtypeService goodtypeService;
    @Autowired
    private YibaopaymentService yibaopaymentService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderprofitdetailService orderprofitdetailService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private SetleaderlevelService setleaderlevelService;
    @Autowired
    private UserorderexcelService userorderexcelService;


    @Autowired
    public void setfaultRecordDao(UserorderDao userorderDao) {
        this.userorderDao = userorderDao;
        super.setBaseDAO(userorderDao);
    }

    @Override
    public void getUserordermsg(List<Userorder> userOrderList) {
        if (CollectionUtils.isEmpty(userOrderList)) {
            return;
        }
        userOrderList.stream().forEach(userOrder -> {
            List<Ordergoods> orderGoodsList = ordergoodsService.listByOrderId(userOrder.getId());
            //计算商品数量
            AtomicInteger goodsCount = new AtomicInteger(0);
            orderGoodsList.stream().forEach(goods -> {
                goodsCount.addAndGet(goods.getCount());
            });
            userOrder.setCount(goodsCount.get());
            userOrder.setOrdergoodsList(orderGoodsList);
        });
    }

    @Override
    public void getUserordermsg(Userorder userorder) {
        // TODO Auto-generated method stub
        if (userorder != null) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put(Common.DELSTATUS, DelStatus.NODEL);
            params.put("orderId", userorder.getId());
            List<Ordergoods> ordergoodsList = ordergoodsService.getByParams(params);
            //计算商品数量
            Integer count = 0;
            if (ordergoodsList != null && ordergoodsList.size() > 0) {
                for (Ordergoods ordergoods : ordergoodsList) {
                    if (ordergoods.getCount() != null) {
                        count += ordergoods.getCount();
                    }
                }
            }
            userorder.setCount(count);
            userorder.setOrdergoodsList(ordergoodsList);
            Orderaftersale orderaftersale = orderaftersaleService.getByParam(params);
            userorder.setOrderaftersale(orderaftersale);
            params = new HashMap<String, Object>();
            params.put(Common.DELSTATUS, DelStatus.NODEL);
            Map<String, Object> searchParams = new HashMap<String, Object>();
            searchParams.put("orderNos", userorder.getOrderNo());
            Yibaopayment yibaopayment = yibaopaymentService.getByParam(params, searchParams, null);
            if (yibaopayment != null) {
                userorder.setTopOrderNo(yibaopayment.getSerialNumber());
            }
        }
    }

    @Override
    public void improtExpressExcel(CommonsMultipartFile multipartFile, Workbook book)
            throws ParseException, BiffException, IOException {
        // TODO Auto-generated method stub
        WorkbookSettings workbookSettings = new WorkbookSettings();
        workbookSettings.setEncoding("UTF-8");
        book = Workbook.getWorkbook(multipartFile.getInputStream(), workbookSettings);
        //读取工作簿
        Sheet sheet = book.getSheet(0);//获取第一个sheet
        Integer row = 1;//从第二行开始
        Cell cell = null;
        List<ExceloutResult> exceloutResultList = new ArrayList<ExceloutResult>();
        while (sheet.getRows() > row) {
            //订单号
            cell = sheet.getCell(0, row);
            if (cell.getContents() == null || cell.getContents().trim().length() == 0) {
                exceloutResultList.add(new ExceloutResult(cell.getContents(), null, null, 1));
                row++;
                continue;
            }
            String orderNo = cell.getContents().trim();
            Map<String, Object> prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("orderNo", orderNo.trim());
            prams.put(Common.STATUS, Arrays.asList(1, 2));
            Userorder userorder = userorderDao.getObject(prams);
            if (userorder == null) {
                LogUtil.Info("===========订单信息不存在:" + orderNo);
                row++;
                continue;
            }
            //快递公司编码
            cell = sheet.getCell(1, row);
            if (cell.getContents() == null || cell.getContents().trim().length() == 0) {
                row++;
                continue;
            }
            prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("espressNo", cell.getContents().trim());
            Expressnumber express = expressnumberService.getByParam(prams);
            if (express == null) {
                LogUtil.Info("===========快递编码不存在:" + cell.getContents().trim());
                row++;
                continue;
            }
            userorder.setExpress(ObjectUtil.removeAllBlank(express.getName()));
            userorder.setExpressCode(ObjectUtil.removeAllBlank(express.getEspressNo()));
            //快递单号
            cell = sheet.getCell(2, row);
            if (cell.getContents() == null || cell.getContents().trim().length() == 0) {
                row++;
                continue;
            }
            userorder.setExpressNo(cell.getContents().trim());
            if (userorder.getStatus() == 1) {
                userorder.setShipmentStatus(1);
                userorder.setGmtShipment(new Date());
                userorder.setStatus(2);
                userorder.setGmtModify(new Date());
                userorder.setGmtDelivery(new Date());
            }
            userorderDao.saveOrUpdate(userorder);
            row++;
        }
    }

    @Override
    public void tocreatedExcel(HttpServletRequest request, List<UserorderexcelVo> userorderexcelList) {
        // TODO Auto-generated method stub
        try {
            Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
            String fileName = new Date().getTime() + ".xls";//文件名
            String fileUrl = FileUploadUtil.getUploadUrl(request, "filedownload" + File.separator + fileName);//生成路径
            WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
            WritableSheet sheet1 = workbook.createSheet("Sheet1", 0);
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat format = new WritableCellFormat();
            format.setAlignment(Alignment.CENTRE);
            format.setVerticalAlignment(VerticalAlignment.CENTRE);
            Label label = new Label(0, 0, "主订单号", format);
            sheet1.addCell(label);
            label = new Label(1, 0, "订单号", format);
            sheet1.addCell(label);
            label = new Label(2, 0, "下单时间", format);
            sheet1.addCell(label);
            label = new Label(3, 0, "商家名称", format);
            sheet1.addCell(label);
            label = new Label(4, 0, "商家编码", format);
            sheet1.addCell(label);
            label = new Label(5, 0, "状态", format);
            sheet1.addCell(label);
            label = new Label(6, 0, "序号", format);
            sheet1.addCell(label);
            label = new Label(7, 0, "商品名称", format);
            sheet1.addCell(label);
            label = new Label(8, 0, "商品代码", format);
            sheet1.addCell(label);
            label = new Label(9, 0, "分类编码", format);
            sheet1.addCell(label);
            label = new Label(10, 0, "规格", format);
            sheet1.addCell(label);
            label = new Label(11, 0, "商品市场价", format);
            sheet1.addCell(label);
            label = new Label(12, 0, "商品售价", format);
            sheet1.addCell(label);
            label = new Label(13, 0, "支付金额", format);
            sheet1.addCell(label);
            label = new Label(14, 0, "数量", format);
            sheet1.addCell(label);
            label = new Label(15, 0, "顾客姓名", format);
            sheet1.addCell(label);
            label = new Label(16, 0, "顾客手机", format);
            sheet1.addCell(label);
            label = new Label(17, 0, "配送方式", format);
            sheet1.addCell(label);
            label = new Label(18, 0, "快递公司", format);
            sheet1.addCell(label);
            label = new Label(19, 0, "快递单号", format);
            sheet1.addCell(label);
            label = new Label(20, 0, "收货地址", format);
            sheet1.addCell(label);
            UserorderexcelVo userorderexcel = null;
            for (int i = 0, j = 1; i < userorderexcelList.size(); i++, j++) {
                userorderexcel = userorderexcelList.get(i);
                ObjectUtil.ToChangeNullToEmpty(userorderexcel);
                label = new Label(0, j, userorderexcel.getTopOrderNo(), format);
                sheet1.addCell(label);
                label = new Label(1, j, userorderexcel.getOrderNo(), format);
                sheet1.addCell(label);
                label = new Label(2, j, DateUtil.simpdfyMdHms.format(userorderexcel.getGmtOrder()), format);
                sheet1.addCell(label);
                label = new Label(3, j, userorderexcel.getMerchantName(), format);
                sheet1.addCell(label);
                label = new Label(4, j, userorderexcel.getMerchantCode(), format);
                sheet1.addCell(label);
                label = new Label(5, j, ObjectUtil.getOrderState(userorderexcel.getStatus()), format);
                sheet1.addCell(label);
                label = new Label(6, j, String.valueOf(userorderexcel.getOrderId()), format);
                sheet1.addCell(label);
                label = new Label(7, j, userorderexcel.getGoodsName(), format);
                sheet1.addCell(label);
                label = new Label(8, j, userorderexcel.getGoodsNo(), format);
                sheet1.addCell(label);
                label = new Label(9, j, goodtypeService.getGoodsTypeByGoodsId(userorderexcel.getGoodsId(), 0), format);
                sheet1.addCell(label);
                label = new Label(10, j, userorderexcel.getSpecification(), format);
                sheet1.addCell(label);
                DecimalFormat ecPrice = new DecimalFormat("0.00");
                label = new Label(11, j, ecPrice.format(userorderexcel.getEcPrice()), format);
                sheet1.addCell(label);
                DecimalFormat salePrice = new DecimalFormat("0.00");
                if (userorderexcel.getSalePrice() != null) {
                    label = new Label(12, j, salePrice.format(userorderexcel.getSalePrice()), format);
                    sheet1.addCell(label);
                }
                DecimalFormat payAmount = new DecimalFormat("0.00");
                label = new Label(13, j, payAmount.format(userorderexcel.getPayableAmount()), format);
                sheet1.addCell(label);
                label = new Label(14, j, String.valueOf(userorderexcel.getCount()), format);
                sheet1.addCell(label);
                label = new Label(15, j, userorderexcel.getConsignee(), format);
                sheet1.addCell(label);
                label = new Label(16, j, userorderexcel.getPhone(), format);
                sheet1.addCell(label);
                label = new Label(17, j, userorderexcel.getDeliveryType(), format);
                sheet1.addCell(label);
                label = new Label(18, j, userorderexcel.getExpress(), format);
                sheet1.addCell(label);
                label = new Label(19, j, userorderexcel.getExpressNo(), format);
                sheet1.addCell(label);
                String address = userorderexcel.getProvince() + userorderexcel.getCity() + userorderexcel.getDistrict() + userorderexcel.getAddress();
                label = new Label(20, j, address, format);
                sheet1.addCell(label);
            }
            workbook.write();
            workbook.close();
//				fileUrl="filedownload/"+fileName;
            InputStream inStream = new FileInputStream(fileUrl);
            byte[] data = ObjectUtil.readInputStream(inStream);
            String dowmupload = OSSUtil.upload(data, ".xls");
            if (dowmupload != null && dowmupload.length() > 0) {
                userorderexcelService.saveOrUpdate(new Userorderexcel(sysuser.getId(), 0, dowmupload));
            }
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    @Override
    public List<Userorder> getUserorderList(Map<String, Object> params, Map<String, Object> searchParams,
                                            Map<String, Object> otherParams, Map<String, Object> merchantParams, Map<String, Object> sortParams,
                                            Integer pageIndex, Integer pageNum, Map<String, Object> gtParams, Map<String, Object> ltParams) {
        // TODO Auto-generated method stub
        return userorderDao.getUserorderList(params, searchParams, otherParams, merchantParams, sortParams,
                pageIndex, pageNum, gtParams, ltParams);
    }

    @Override
    public List<Long> getUserorderByGoodsNo(String goodsNo) {
        // TODO Auto-generated method stub
        List<Long> orderIdList = null;
        if (goodsNo != null && goodsNo.length() > 0) {
            Map<String, Object> prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("goodsNo", goodsNo);
            List<Ordergoods> ordergoodsList = ordergoodsService.getByParams(prams);
            if (ordergoodsList != null && ordergoodsList.size() > 0) {
                orderIdList = new ArrayList<Long>();
                for (Ordergoods ordergoods : ordergoodsList) {
                    orderIdList.add(ordergoods.getOrderId());
                }
            }
        }
        return orderIdList;
    }

    @Override
    public List<Userorder> getAllUserorderList(Map<String, Object> params, Map<String, Object> searchParams,
                                               Map<String, Object> otherParams, Map<String, Object> merchantParams, Map<String, Object> sortParams,
                                               Map<String, Object> gtParams, Map<String, Object> ltParams) {
        // TODO Auto-generated method stub
        return userorderDao.getAllUserorderList(params, searchParams, otherParams, merchantParams, sortParams, gtParams, ltParams);
    }

    @Override
    public String getUserorderByTopOrderNo(String orderNo) {
        // TODO Auto-generated method stub
        Map<String, Object> prams = new HashMap<String, Object>();
        prams.put(Common.DELSTATUS, DelStatus.NODEL);
        prams.put("serialNumber", orderNo);
        Yibaopayment yibaopayment = yibaopaymentService.getByParam(prams);
        if (yibaopayment != null) {
            return yibaopayment.getOrderNos();
        }
        return null;
    }

    @Override
    public Integer getUserorderCount(Map<String, Object> params, Map<String, Object> searchParams,
                                     Map<String, Object> otherParams, Map<String, Object> merchantParams, Map<String, Object> gtParams,
                                     Map<String, Object> ltParams) {
        // TODO Auto-generated method stub
        return userorderDao.getUserorderCount(params, searchParams, otherParams, merchantParams, gtParams, ltParams);
    }

    @Override
    public String toExpressExcel(HttpServletRequest request, List<Userorder> userorderList) {
        // TODO Auto-generated method stub
        try {
            String fileName = "expressNo" + new Date().getTime() + "" + SmsUtil.getRandom(3) + ".xls";//文件名
            String fileUrl = FileUploadUtil.getUploadUrl(request, "filedownload" + File.separator + fileName);//生成路径
            WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
            WritableSheet sheet1 = workbook.createSheet("Sheet1", 0);
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat format = new WritableCellFormat();
            format.setAlignment(Alignment.CENTRE);
            format.setVerticalAlignment(VerticalAlignment.CENTRE);
            Label label = new Label(0, 0, "订单号", format);
            sheet1.addCell(label);
            label = new Label(1, 0, "物流公司编码", format);
            sheet1.addCell(label);
            label = new Label(2, 0, "物流单号", format);
            sheet1.addCell(label);
            Userorder userorder = null;
            for (int i = 0, j = 1; i < userorderList.size(); i++, j++) {
                userorder = userorderList.get(i);
                ObjectUtil.ToChangeNullToEmpty(userorder);
                label = new Label(0, j, userorder.getOrderNo(), format);
                sheet1.addCell(label);
            }
            workbook.write();
            workbook.close();
            fileUrl = "filedownload/" + fileName;
            return fileUrl;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void getWithdrawUserorderMsg(List<Userorder> userorderList) {
        DecimalFormat amount = new DecimalFormat("0.00");
        if (CollectionUtils.isEmpty(userorderList)) {
            return;
        }
        Double leaderRatio = (setleaderlevelService.getLeaderRate(1) + setleaderlevelService.getLeaderRate(2)) * 0.01;
        for (Userorder userorder : userorderList) {
            Map<String, Object> prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("orderNo", userorder.getOrderNo());
            Orderprofitdetail orderprofitdetail = orderprofitdetailService.getByParam(prams);
            if (orderprofitdetail != null) {
                userorder.setMerchantAmount(orderprofitdetail.getMerAmount());
                userorder.setPlatformAmount(orderprofitdetail.getPlatAmount());
            }
            prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put(Common.ID, userorder.getMerchantId());
            Merchant merchant = merchantService.getByParam(prams);
            Double merchantRatio = 1 - (merchant.getCommissionRatio() * 0.01);
            if (merchant != null) {
                userorder.setMerchantRatio(ObjectUtil.sciCal(merchantRatio * 100, 2));
                Double platformRatio = 1 - merchantRatio - leaderRatio;
                userorder.setPlatformRatio(ObjectUtil.sciCal(platformRatio * 100, 2));
            }
            //查询订单明细
            prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("orderId", userorder.getId());
            List<Ordergoods> orderGoodList = ordergoodsService.getByParams(prams);
            userorder.setOrdergoodsList(orderGoodList);
        }
    }

    @Override
    public void getWithdrawUserorderMsg(Userorder userorder) {
        // TODO Auto-generated method stub
        DecimalFormat amount = new DecimalFormat("0.00");
        if (userorder != null) {
            Double leaderRatio = setleaderlevelService.getLeaderRate(1) + setleaderlevelService.getLeaderRate(2);
            Map<String, Object> prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("orderNo", userorder.getOrderNo());
            Orderprofitdetail orderprofitdetail = orderprofitdetailService.getByParam(prams);
            if (orderprofitdetail != null) {
                userorder.setMerchantAmount(orderprofitdetail.getMerAmount());
                userorder.setPlatformAmount(orderprofitdetail.getPlatAmount());
            }
            prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put(Common.ID, userorder.getMerchantId());
            Merchant merchant = merchantService.getByParam(prams);
            if (merchant != null) {
                Double merchantRatio = 1 - (merchant.getCommissionRatio() * 0.01);
                userorder.setMerchantRatio(Double.valueOf(amount.format(merchantRatio * 100)));
                Double platformRatio = 1 - merchantRatio - leaderRatio;
                userorder.setPlatformRatio(Double.valueOf(amount.format(platformRatio * 100)));
            }
            prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("orderId", userorder.getId());
            List<Ordergoods> ordergoodList = ordergoodsService.getByParams(prams);
            userorder.setOrdergoodsList(ordergoodList);
        }
    }

    @Override
    public void toWithdrawUserorderExcel(HttpServletRequest request, List<Userorder> userorderList) {
        // TODO Auto-generated method stub
        try {
            Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
            String fileName = "withdrawuserorder" + new Date().getTime() + "" + SmsUtil.getRandom(3) + ".xls";//文件名
            String fileUrl = FileUploadUtil.getUploadUrl(request, "filedownload" + File.separator + fileName);//生成路径
            WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
            WritableSheet sheet1 = workbook.createSheet("Sheet1", 0);
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"), 10);
            WritableCellFormat format = new WritableCellFormat();
            format.setAlignment(Alignment.CENTRE);
            format.setVerticalAlignment(VerticalAlignment.CENTRE);
            Label label = new Label(0, 0, "订单号", format);
            sheet1.addCell(label);
            label = new Label(1, 0, "供应商名称", format);
            sheet1.addCell(label);
            label = new Label(2, 0, "供应商编码", format);
            sheet1.addCell(label);
            label = new Label(3, 0, "订单总金额", format);
            sheet1.addCell(label);
            label = new Label(4, 0, "供应商分润", format);
            sheet1.addCell(label);
            label = new Label(5, 0, "供应商比例", format);
            sheet1.addCell(label);
            label = new Label(6, 0, "平台分润", format);
            sheet1.addCell(label);
            label = new Label(7, 0, "平台比例", format);
            sheet1.addCell(label);
            label = new Label(8, 0, "供应商提现状态", format);
            sheet1.addCell(label);
            label = new Label(9, 0, "下单时间", format);
            sheet1.addCell(label);
            Userorder userorder = null;
            for (int i = 0, j = 1; i < userorderList.size(); i++, j++) {
                userorder = userorderList.get(i);
                ObjectUtil.ToChangeNullToEmpty(userorder);
                label = new Label(0, j, userorder.getOrderNo(), format);
                sheet1.addCell(label);
                label = new Label(1, j, userorder.getMerchantName(), format);
                sheet1.addCell(label);
                label = new Label(2, j, userorder.getMerchantCode(), format);
                sheet1.addCell(label);
                label = new Label(3, j, String.valueOf(userorder.getAmount()), format);
                sheet1.addCell(label);
                label = new Label(4, j, String.valueOf(userorder.getMerchantAmount()), format);
                sheet1.addCell(label);
                label = new Label(5, j, String.valueOf(userorder.getMerchantRatio()), format);
                sheet1.addCell(label);
                label = new Label(6, j, String.valueOf(userorder.getPlatformAmount()), format);
                sheet1.addCell(label);
                label = new Label(7, j, String.valueOf(userorder.getPlatformRatio()), format);
                sheet1.addCell(label);
                label = new Label(8, j, ObjectUtil.getMerchantStatus(userorder.getMerchantStatus()), format);
                sheet1.addCell(label);
                label = new Label(9, j, DateUtil.simpdfyMdHms.format(userorder.getGmtOrder()), format);
                sheet1.addCell(label);
            }
            workbook.write();
            workbook.close();
//				fileUrl="filedownload/"+fileName;
            InputStream inStream = new FileInputStream(fileUrl);
            byte[] data = ObjectUtil.readInputStream(inStream);
            String dowmupload = OSSUtil.upload(data, ".xls");
            if (dowmupload != null && dowmupload.length() > 0) {
                userorderexcelService.saveOrUpdate(new Userorderexcel(sysuser.getId(), 1, dowmupload));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public List<Userorder> getUserorderByNoIds(Map<String, Object> params, Map<String, Object> noParams,
                                               Map<String, Object> searchParams, Map<String, Object> sortParams, Map<String, Object> gtParams,
                                               Map<String, Object> ltParams) {
        // TODO Auto-generated method stub
        return userorderDao.getUserorderByNoIds(params, noParams, searchParams, sortParams, gtParams, ltParams);
    }

    @Override
    public Userorder getForUpdate(Long id) {
        // TODO Auto-generated method stub
        return userorderDao.getForUpdate(id);
    }


    @Override
    public List<ExportOrderVO> listExportOrderByParam(Map<String, Object> param) {
        List<ExportOrderVO> list = userorderDao.listExportOrderByParam(param);
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }

        list.stream().forEach(order -> {
            // 处理收货地址
            StringBuffer sb = new StringBuffer();
            sb.append(order.getProvince()).append(order.getCity()).append(order.getDistrict()).append(order.getAddress());
            order.setAddress(sb.toString());

            // 订单状态
            order.setStatusTxt(ObjectUtil.getOrderState(order.getStatus()));
        });
        return list;
    }


    @Override
    public List<ExportPlatformOrderVO> getExportPlatformOrder(Map<String, Object> params) {
        List<ExportPlatformOrderVO> exportOrderList = userorderDao.getExportPlatformOrder(params);
        exportOrderList.stream().forEach(order -> {
            order.setMerchantStatusTxt(ObjectUtil.getMerchantStatus(order.getMerchantStatus()));
            order.setPlatformStatusTxt(ObjectUtil.getPlatformApplyStatusNew(order.getPlatformStatus()));
            order.setStatusTxt(ObjectUtil.getOrderState(order.getStatus()));

            double merchantRadio = 85.00;
            if (order.getCommissionRatio() != null) {
                merchantRadio = ArithUtils.sub(100.00, order.getCommissionRatio() == null ? 0 : order.getCommissionRatio());
            }
            order.setMerchantRadio(merchantRadio + "%");

            double leaderRatio = order.getLeaderRatio() == null ? 10.00 : order.getLeaderRatio();
            order.setLeaderRadioTxt(leaderRatio + "%");

            double platformRadio = ArithUtils.sub(ArithUtils.sub(100.00, merchantRadio), leaderRatio);
            order.setPlatformRadio(platformRadio + "%");

            double leaderAmount = ArithUtils.add(order.getFirstAmount() == null ? 0 : order.getFirstAmount(),
                    order.getSecondAmount() == null ? 0 : order.getSecondAmount());
            order.setLeaderAmount(ObjectUtil.sciCal(leaderAmount, 2));

            double temp = ArithUtils.sub(order.getOrderAmount() == null ? 0 : order.getOrderAmount(),
                    order.getServiceCharge() == null ? 0 : order.getServiceCharge());
            order.setPlatformSum(ObjectUtil.sciCal(ArithUtils.sub(temp, order.getMerAmount() == null ? 0 : order.getMerAmount()), 2));

            order.setServiceCharge(order.getServiceCharge() == null ? 0 : order.getServiceCharge());
        });
        return exportOrderList;
    }


    @Override
    public List<ExportMerchantOrderVO> listExportMerchantOrder(Map<String, Object> params) {
        List<ExportMerchantOrderVO> exportOrderList = userorderDao.listExportMerchantOrder(params);
        exportOrderList.stream().forEach(order -> {
            order.setMerchantStatusTxt(ObjectUtil.getMerchantStatus(order.getMerchantStatus()));
            order.setStatusTxt(ObjectUtil.getOrderState(order.getStatus()));
            order.setMerchantRadio(100.00 - order.getCommissionRatio() + "%");
            order.setPlatformRadio(order.getCommissionRatio() - order.getLeaderRatio() + "%");
        });
        return exportOrderList;
    }
}